﻿/********************************************************************************
** 作者： Mick
** 邮箱： zheng_jinfan@126.com
** 主页： http://www.zhengjinfan.cn
** 创始时间：2016-2-17
** 描述：
**      [辅助类]EditorHelp 的摘要说明
*********************************************************************************/
using System.Text.RegularExpressions;

namespace BestEasyCMS.Framework.Util
{
    /// <summary>
    ///EditorHelp 的摘要说明
    /// </summary>
    public class EditorUtil
    {
        /// <summary>
        /// 构建类的实例对象
        /// </summary>
        /// <returns></returns>
        public static EditorUtil Instance { get { return new EditorUtil(); } }

        /// <summary>
        /// 返回分页切割线正则
        /// </summary>
        /// <param name="type">编辑器类型。0:所有已有编辑器； 1：ewebeditor；2：kindEditor；3：ueditor,百度编辑器</param>
        /// <returns></returns>
        public string GetContentPagePattern(int type)
        {
            string reslut =
                "(<!--/ewebeditor:page-->\\s*<!--ewebeditor:page title=\"\"-->)|(_ueditor_page_break_tag_)|(<hr .*[\"']ke-pagebreak[\"'].*/?>)";
            switch (type)
            {
                case 1:
                    reslut = "<!--/ewebeditor:page-->\\s*<!--ewebeditor:page title=\"\"-->";
                    break;
                case 2: //kindEditor
                    reslut = "<hr .*[\"']ke-pagebreak[\"'].*/?>";
                    break;
                case 3: //ueditor，百度编辑器
                    reslut = "_ueditor_page_break_tag_";
                    break;
            }
            return reslut;
        }



        /// <summary>
        /// 根据编辑器转换分页符
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="type">编辑器类型。 1：ewebeditor；2：kindEditor；3：ueditor,百度编辑器</param>
        /// <returns></returns>
        public string ChangeContent(string content, int type)
        {
            var pattern = GetContentPagePattern(0);
            string replacement;
            switch (type)
            {
                case 1:
                    if (Regex.IsMatch(content, GetContentPagePattern(1)))
                        break;
                    replacement = "<!--/ewebeditor:page-->\n<!--ewebeditor:page title=\"\"-->";
                    content = Regex.Replace(content, pattern, replacement, RegexOptions.IgnoreCase);

                    content = "<!--ewebeditor:page title=\"\"-->" + content + "<!--/ewebeditor:page-->";
                    break;
                case 2: //kindEditor
                    if (Regex.IsMatch(content, GetContentPagePattern(2)))
                        break;
                    replacement = "<hr style=\"page-break-after:always;\" class=\"ke-pagebreak\">";
                    content = Regex.Replace(content, pattern, replacement, RegexOptions.IgnoreCase);
                    content = ClearEwebeditorContentPage(content);
                    break;
                case 3: //ueditor，百度编辑器
                    if (Regex.IsMatch(content, GetContentPagePattern(3)))
                        break;
                    replacement = "_ueditor_page_break_tag_";
                    content = Regex.Replace(content, pattern, replacement, RegexOptions.IgnoreCase);
                    content = ClearEwebeditorContentPage(content);
                    break;
            }
            return content;
        }

        /// <summary>
        /// 清除Ewebeditor编辑器分页符痕迹
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public string ClearEwebeditorContentPage(string content)
        {
            content = Regex.Replace(content, "<!--ewebeditor:page title=\"\"-->", "", RegexOptions.IgnoreCase);
            content = Regex.Replace(content, "<!--/ewebeditor:page-->", "", RegexOptions.IgnoreCase);
            return content;
        }
    }
}